﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.MO.Enums;
using UFIDA.U9.SM.SO;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForMOtoSODeleted : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //生产订单
            UFIDA.U9.MO.MO.MO mo = key.GetEntity() as UFIDA.U9.MO.MO.MO;
            if (mo == null) return;

            #region 生产订单新增时,回写生产数量到销售订单
            //判断来源单据是否为空
            if (mo.OriginalData.SrcDoc.SrcDocNo != null && mo.OriginalData.SrcDoc.SrcDocNo != "" && mo.OriginalData.SrcDoc.MOSrcDocType == MOSrcDocTypeEnum.SO)
            {
                //引入销售订单
                SO so = SO.Finder.FindByID(mo.OriginalData.SrcDoc.SrcDoc.ID);
                string sql = $@"select sum(mo.ProductQty) from MO_MO mo
                                where mo.SrcDoc_SrcDoc_EntityID = '{mo.OriginalData.SrcDoc.SrcDoc.ID}'
                                    group by mo.SrcDoc_SrcDoc_EntityID";
                DataSet ds = null;
                DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);

                foreach (var soLine in so.SOLines)
                {

                    if (soLine.DocLineNo == int.Parse(mo.OriginalData.SrcDoc.SrcDocLineNo.ToString()))
                    {
                        if (ds.Tables[0].Rows.Count < 1 )
                        {
                            string sql1 = $@"update SM_SOLine set DescFlexField_PrivateDescSeg2  = '0'
                                                from SM_SOLine line 
                                                left join SM_SO so on so.ID = line.SO 
                                                where so.DocNo  = '{so.DocNo}' and line.DocLineNo ='{soLine.DocLineNo}'";
                            DataSet ds1 = null;
                            DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);

                        }
                        else
                        {
                            string sql1 = $@"update SM_SOLine set DescFlexField_PrivateDescSeg2  = '{ds.Tables[0].Rows[0][0]}'
                                                from SM_SOLine line 
                                                left join SM_SO so on so.ID = line.SO 
                                                where so.DocNo  = '{so.DocNo}' and line.DocLineNo ='{soLine.DocLineNo}'";
                            DataSet ds1 = null;
                            DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);
                        }


                        }
                   
                }
            }

                #endregion

            }

    }
}
